﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Configuration;


namespace MVCCentral.Framework.Logger
{
    public class SqlLogger : ILogger
    {
        public void Log(LogEventArgs args)
        {
            string connName = ConfigurationManager.AppSettings.Get("connName");
            string connection = ConfigurationManager.ConnectionStrings[connName].ConnectionString;

            // Database routines can go here.
            DbProviderFactory df = DbProviderFactories.GetFactory("System.Data.SqlClient");
            DbConnection cn = df.CreateConnection();
            cn.ConnectionString = connection;
            cn.Open();

            string cmdText = "INSERT INTO ErrorLog (Date, MethodName, ErrorMessage, StackTrace, Source, TargetSite)";
            cmdText += " VALUES ('" + DateTime.Now.ToString() + "','" + args.Source + "','" + args.Message + "','" + args.Exception.StackTrace + "','";
            cmdText += args.Exception.Source + "','" + args.Exception.TargetSite.ToString() + "')";

            DbCommand cmd = df.CreateCommand();
            cmd.Connection = cn;
            cmd.CommandText = cmdText;

            cmd.ExecuteNonQuery();
            cmd.Dispose();

            cn.Close();
        }

    }
}
